﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Caching;

namespace ZBJF.Warranty.Common.Common
{
    public class CacheHelper
    {
        private static CacheHelper _instance;

        public static CacheHelper Instance
        {
            get { return _instance ?? (_instance = new CacheHelper()); }
        }
        private static Cache _cache;

        private static Cache WebCache
        {
            get
            {
                HttpContext context = HttpContext.Current;
                if (context != null)
                {
                    _cache = (Cache)context.Cache;
                }
                else
                {
                    _cache = HttpRuntime.Cache;
                }
                return _cache;
            }
        }
        private static readonly object UnitsLockObject = new object();
        /// <summary>
        /// 从缓存中得到数据，如果不存在，则将数据插入缓存中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cacheKey"></param>
        /// <param name="refresh"></param>
        /// <param name="action"></param>
        /// <param name="cacheHours"></param>
        /// <returns></returns>
        public T GetDataFromCache<T>(string cacheKey, Func<T> action, bool refresh = false, double cacheHours = 1)
        {
            var items = (T)WebCache.Get(cacheKey);

            if (items == null || refresh)
            {
                lock (UnitsLockObject)
                {
                    items = (T)WebCache.Get(cacheKey);

                    if (items == null || refresh)
                    {
                        items = action();
                        WebCache.Insert(cacheKey, items, null, DateTime.Now.AddHours(cacheHours), Cache.NoSlidingExpiration);
                    }
                }
            }

            return items;
        }

        /// <summary>
        /// 获取数据缓存
        /// </summary>
        /// <param name="cacheKey">键</param>
        public object GetCache(string cacheKey)
        {
            return WebCache[cacheKey];
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public void SetCache(string cacheKey, object objObject)
        {
            WebCache.Insert(cacheKey, objObject);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public void SetCache(string cacheKey, object objObject, TimeSpan timeout)
        {
            WebCache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
        }

        /// <summary>
        /// 设置数据缓存
        /// </summary>
        public void SetCache(string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            WebCache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
        }

        /// <summary>
        /// 移除指定数据缓存
        /// </summary>
        public void RemoveCache(string cacheKey)
        {
            WebCache.Remove(cacheKey);
        }

        /// <summary>
        /// 移除全部缓存
        /// </summary>
        public void RemoveAllCache()
        {
            IDictionaryEnumerator cacheEnum = WebCache.GetEnumerator();
            while (cacheEnum.MoveNext())
            {
                if (cacheEnum.Key != null) WebCache.Remove(cacheEnum.Key.ToString());
            }
        }
    }
}
